<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>eureka api 实现</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <!-- Data Tables -->
    <link href="css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content  fadeInRight">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>基本 </h5>
            </div>
            <div class="ibox-content">

                <table class="table table-striped table-bordered table-hover dataTables-example " id="editable">
                    <thead>
                    <tr>
                        <th>Application</th>
                        <th>instanceId</th>
                        <th>status</th>
                        <th>actionType</th>
                        <th>overriddenstatus</th>
                        <th>lastDirtyTimestamp</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>


                </table>

            </div>
        </div>
    </div>
</div>

<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>


<!--<script src="js/plugins/jeditable/jquery.jeditable.js"></script>-->
<!-- Data Tables -->
<script src="js/plugins/dataTables/jquery.dataTables.js"></script>
<script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>


<!-- Page-Level Scripts -->
<script>
    var hostName = "";
    $(document).ready(function () {
        var oTable = $('#editable').dataTable({
            "pageLength": 50,
            "ajax": {
                "url": hostName + "/eureka/apps",
                "dataSrc": function (json) {

                    //Make your callback here.
                    var listServer = json.applications.application;
                    var resultArray = [];
                    for (var i = 0; i < listServer.length; i++) {
                        var serverBean = listServer[i];
                        var instances = serverBean.instance;

                        for (var j = 0; j < instances.length; j++) {
                            resultArray.push(instances[j]);
                        }
                    }
                    console.log(resultArray)
                    return resultArray;
                }
            },
            "columns": [
                {"data": "secureVipAddress"},
                {"data": "instanceId"},
                {"data": "status"},
                {"data": "actionType"},
                {"data": "overriddenstatus"},
                {
                    "data": function (obj) {
                        console.log(obj.lastUpdatedTimestamp);
                        return getMyDate(obj.lastDirtyTimestamp);

                    }
                },
                {data: null}

            ],
            "columnDefs": [{
                targets: 6,
                render: function (data, type, row, meta) {
                    return '&nbsp;&nbsp;<a type="button" class="btn btn-danger " href="#" onclick=delFromID("' + row.instanceId + '","' + row.secureVipAddress + '","OUT_OF_SERVICE") >暂停服务</a>' +
                        '&nbsp;&nbsp;&nbsp;&nbsp;<a type="button" class="btn btn-danger " href="#" onclick=delFromID("' + row.instanceId + '","' + row.secureVipAddress + '","UP") >启动服务</a>'

                        ;
                }
            },
                {"orderable": false, "targets": 6},
            ]
        });


    });

    function delFromID(id, app, status) {
        console.log(app);
        $.ajax({
            url: hostName + "/eureka/apps/" + app + "/" + id + "/status?value=" + status,
            type: "PUT",
            success: function (result) {
                console.log(result)
            }
        });
    }

    //将时间戳格式化
    function getMyDate(time) {
        if (typeof(time) == "undefined") {
            return "";
        }
        var oDate = new Date(parseInt(time));
        var oYear = oDate.getFullYear();
        var oMonth = oDate.getMonth() + 1;
        var oDay = oDate.getDate();
        var oHour = oDate.getHours();
        var oMin = oDate.getMinutes();
        var oSen = oDate.getSeconds();
        var oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen);//最后拼接时间
        return oTime;
    };

    //补0操作,当时间数据小于10的时候，给该数据前面加一个0
    function getzf(num) {
        if (parseInt(num) < 10) {
            num = '0' + num;
        }
        return num;
    }
</script>
</body>

</html>
